<%@page import="java.util.Date"%>
<%@page import="java.util.Comparator"%>
<%@page import="org.imperativetable.Column"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.imperativetable.i18n.MessageResources"%>
<%@page import="org.imperativetable.ColumnContent"%>
<%@page import="org.imperativetable.Table"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="test.TestClass"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Imperative Table</title>
    </head>
    <body>
        <h1>Imperative Table</h1>
        <%
                    List<TestClass> list = new ArrayList<TestClass>();
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());
                    list.add(new TestClass());

                    long time = System.currentTimeMillis();

                    Table<TestClass> table = new Table<TestClass>();
                    table.setData(list);

                    table.addColumn(new Column.Builder("Date", new ColumnContent<TestClass>() {
                        public Object getValue(TestClass rowObject, MessageResources messageResources) {
                            DateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
                            return sdf.format(rowObject.getDate());
                        }
                    }).loadTitleFromMessageResources(true).sotrable(true).comparator(new Comparator<String>() {
                        public int compare(String o1, String o2) {
                            DateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
                            try {
                                return sdf.parse(o1).compareTo(sdf.parse(o2));
                            } catch(Exception e) {
                                return o1.compareTo(o2);
                            }
                        }
                    }).build());
                    
                    table.addColumn(new Column.Builder("Price", new ColumnContent<TestClass>() {
                        public Object getValue(TestClass rowObject, MessageResources messageResources) {
                            return rowObject.getPrice();
                        }
                    }).loadTitleFromMessageResources(true).sotrable(true).build());
                    
                    table.addColumn(new Column.Builder("Count", new ColumnContent<TestClass>() {
                        public Object getValue(TestClass rowObject, MessageResources messageResources) {
                            return rowObject.getCount();
                        }
                    }).loadTitleFromMessageResources(true).sotrable(true).build());
                    
                    table.addColumn(new Column.Builder("Total", new ColumnContent<TestClass>() {
                        public Object getValue(TestClass rowObject, MessageResources messageResources) {
                            return "$ " + rowObject.getTotal();
                        }
                    }).loadTitleFromMessageResources(true).sotrable(true).build());

                    table.render(request, out);

                    out.println("duration: " + (System.currentTimeMillis() - time) + " ms");
        %>
    </body>
</html>
